package com.kiln.juxingqs.ui

import android.text.TextUtils
import androidx.lifecycle.Observer
import client.global.commlib.dialog.CameraOrSelectDialog
import client.global.commlib.toast.toast
import client.global.commlib.utils.StoreUtil
import com.kiln.juxingqs.R
import com.kiln.juxingqs.api.bean.UserInfo
import com.kiln.juxingqs.databinding.ActivitySetrealBinding
import com.kiln.juxingqs.ui.vm.SetRealViewModel
import com.kiln.juxingqs.BaseMiddleActivity

class SetRealActivity : BaseMiddleActivity<SetRealViewModel, ActivitySetrealBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_setreal
    }

    override fun initView() {
        binding?.apply {
            event = this@SetRealActivity
            vm = viewModel
            StoreUtil.getBean<UserInfo>(this@SetRealActivity)?.let {
                viewModel.userInfo.value = it
                inconme = it.income.toString()
            }
        }
    }

    override fun observerUI() {
        viewModel.call.observe(this, Observer {
            if (it == null) {
                toast("网络错误")
            } else {
                it?.apply {
                    if (isSuccess) {
                        toast("提交成功")
                        finish()
                    } else {
                        toast(it.message)
                    }
                }
            }
        })

    }

    override fun initViewModel(): SetRealViewModel = createViewModel()

    fun selectImg(index: Int) {
        CameraOrSelectDialog(this).apply {
            suc = {
                when (index) {
                    0 -> {
                        viewModel.fontUrl.value = null
                        viewModel.idFontImg.value = it
                        mImgLoader.load(it).into(this@SetRealActivity.binding.idFontImg)
                    }
                }
            }
        }.show()
    }

    fun ok() {
        if (TextUtils.isEmpty(viewModel.realname.value)
            || viewModel.idcard.value.isNullOrEmpty()
            || viewModel.sex.value!! < 0
            || (viewModel.fontUrl.value.isNullOrEmpty() && viewModel.idFontImg.value == null)
        ) {
            toast("所有不能为空")
            return
        }

        viewModel.ok(this)
    }

}